package com.xxbb.filesubmit.controller;

import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.runtime.Log;

import javax.xml.crypto.Data;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

@RestController
@RequestMapping("file")
public class FileController {
    @PostMapping("upload")
    public Boolean Upload(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName, @RequestParam("count") String count, @RequestParam("index") String index) {
        FileOutputStream out = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        String nowTimeStr = df.format(new Date());

        String uploadPath = "C:\\FileUpload\\file\\FileSubmit\\" + nowTimeStr + "\\";
        File f = new File(uploadPath);
        if (!f.exists()) {
            f.mkdirs();
        }
        try {
            if (Integer.parseInt(index) == 0) {
                out = new FileOutputStream(uploadPath + fileName);
            } else {
                out = new FileOutputStream(uploadPath + fileName, true);
            }
            out.write(file.getBytes());
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
